<template>
    <div class="edu-page-warp">
        <div v-if="hasPlan">
            <el-card
                shadow="never"
                body-style="padding: 24px;"
                class="card-margin"
            >
                <div class="no-culture-plan"/>
                <div style="text-align:center;margin-bottom:100px">
                    {{ $t("culturePlan.noCultureTip") }}
                </div>
            </el-card>
        </div>
        <div v-else>
            <div v-if="isRead">
                <el-card
                    shadow="never"
                    body-style="padding: 24px;"
                    class="card-margin"
                >
                    <h1 class="condition-title">{{ $t('common.text.baseInfo') }}</h1>
                    <div class="plan-photo">
                        <img src="/static/images/manage.png" >
                        <span>
                            {{ studentCultureRel ? language=='en'?studentCultureRel.nameSpelling:studentCultureRel.name : "" }}
                        </span>
                    </div>
                    <el-form label-position="right">
                        <el-row :gutter="24">
                            <el-col :span="6">
                                <el-form-item :label="$t('culturePlan.stuNumber')+'：'">{{
                                    studentCultureRel
                                        ? studentCultureRel.studentId
                                        : ""
                                }}</el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item
                                    :label="$t('label.grade') + '：'"
                                >{{
                                    studentCultureRel
                                        ? studentCultureRel.grade
                                        : ""
                                }}</el-form-item
                                >
                            </el-col>
                            <el-col :span="6">
                                <el-form-item
                                    :label="$t('label.college') + '：'"
                                >{{
                                    studentCultureRel
                                        ? studentCultureRel.departmentId
                                        : ""
                                }}</el-form-item
                                >
                            </el-col>
                            <el-col :span="6">
                                <el-form-item
                                    :label="$t('label.major') + '：'"
                                >{{
                                    studentCultureRel
                                        ? studentCultureRel.major
                                        : ""
                                }}</el-form-item
                                >
                            </el-col>
                        </el-row>
                        <el-row :gutter="24">
                            <el-col :span="6">
                                <el-form-item
                                    :label="$t('label.trainingLevel') + '：'"
                                >{{
                                    studentCultureRel
                                        ? studentCultureRel.trainingLevel
                                        : ""
                                }}</el-form-item
                                >
                            </el-col>
                            <el-col :span="6">
                                <el-form-item :label="$t('label.enrolDate')+'：'">{{
                                    studentCultureRel
                                        ? studentCultureRel.enrolDate.substring(
                                            0,
                                            10
                                        )
                                        : ""
                                }}</el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item
                                    :label="$t('culturePlan.durationStudy')+'：'"
                                >{{
                                    studentCultureRel
                                        ? studentCultureRel.lengthSchooling
                                        : ""
                                }}{{ $t('culturePlan.year') }}</el-form-item
                                >
                            </el-col>
                            <el-col :span="6">
                                <el-form-item :label="$t('culturePlan.supervisor')+'：'">
                                    {{ language=='en'?(studentCultureRel?studentCultureRel.teacherNameEn:''):(studentCultureRel?studentCultureRel.teacherName:'') }}
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="24">
                            <el-col :span="6">
                                <el-form-item
                                    :label="$t('message.cultureScheme') + '：'"
                                >
                                    <template
                                        v-if="
                                            studentCultureRel &&
                                                studentCultureRel.cultureName
                                        "
                                    >
                                        <el-button
                                            type="text"
                                            size="mini"
                                            @click="
                                                culturePreview(
                                                    studentCultureRel
                                                )
                                            "
                                        >{{ language=='en'?studentCultureRel.cultureNameEn:studentCultureRel.cultureName }}</el-button
                                        >
                                    </template>
                                    <template v-else>{{
                                        $t("culturePlan.noCulture")
                                    }}</template>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </el-card>
                <el-card
                    shadow="never"
                    body-style="padding: 24px;"
                    class="card-margin"
                >
                    <div class="edu-page-title mg-top">
                        <el-row>
                            <el-col>{{ $t('culturePlan.training_plan_my') }}</el-col>
                        </el-row>
                        <el-row>
                            <el-col
                                v-if="
                                    showOperateButton
                                        ? showOperateButton
                                        : studentCultureRel ||
                                            (studentCultureRel &&
                                            studentCultureRel.projId == 1)
                                            ? showOperateButton
                                            : showBtn
                                "
                            >
                                <div
                                    v-if="studentCultureRel && success"
                                    style="float:right;padding:20px 0;"
                                >
                                    <template
                                        v-if="
                                            statusPlan != -1 &&
                                                (isGraduate || isWorked)
                                        "
                                    >
                                        <el-button
                                            v-if="isChecking && !readOnly"
                                            @click="recallAppro"
                                        >{{ $t("culturePlan.recallAppro") }}</el-button
                                        >
                                        <el-button
                                            v-if="!readOnly"
                                            @click="explorerPlan"
                                        >{{ $t("culturePlan.explorerPlan") }}</el-button
                                        >
                                        <el-button
                                            v-if="!readOnly"
                                            @click="explorerWordsPlan"
                                        >{{ $t("culturePlan.explorerWordsPlan") }}</el-button
                                        >
                                    </template>
                                    <template
                                        v-if="
                                            (role == 1 &&
                                                timeOver.length > 0) ||
                                                role == 3 ||
                                                role == 4
                                        "
                                    >
                                        <el-button
                                            v-if="
                                                !readOnly &&
                                                    !isChecking &&
                                                    !customized
                                            "
                                            type="primary"
                                            @click="customPlan"
                                        >{{ $t("culturePlan.createPlan") }}</el-button
                                        >
                                        <el-button
                                            v-if="
                                                !readOnly &&
                                                    !isChecking &&
                                                    customized
                                            "
                                            type="primary"
                                            @click="customPlan"
                                        >{{ $t("culturePlan.modifyPlan") }}</el-button
                                        >
                                    </template>
                                </div>
                            </el-col>
                        </el-row>
                    </div>
                    <div v-if="studentCultureRel">
                        <!--已绑定培养方案，且成功加载数据-->
                        <div v-if="studentCultureRel.cultureId && success">
                            <graduate-plan
                                v-if="isGraduate"
                                :data="data"
                                :student-culture-rel="studentCultureRel"
                                :old4m3="old4m3"
                                prev="true"
                            />
                            <graduateWorkedPlan
                                v-if="isWorked"
                                :data="data"
                                :student-culture-rel="studentCultureRel"
                                :old4m3="old4m3"
                                prev="true"
                            />
                            <undergraduate-plan
                                v-if="!isGraduate && !isWorked"
                                :data="data"
                                :student-culture-rel="studentCultureRel"
                                prev="true"
                            />
                        </div>
                        <!--未绑定培养方案-->
                        <div v-if="!studentCultureRel.cultureId || hasPlan">
                            <div class="no-culture-plan"/>
                            <div style="text-align:center;margin-bottom:100px">
                                {{ $t("culturePlan.noCultureTip") }}
                            </div>
                        </div>
                    </div>
                </el-card>
                <el-card
                    shadow="never"
                    body-style="padding: 24px;"
                    class="card-margin"
                >
                    <h1 class="condition-title">{{ $t("culturePlan.executionPlan") }}</h1>
                    <scoreStatistics
                        v-if="success"
                        :labels="labels"
                        :courses="courses"
                        :new-label="newLabel"
                        :culture-scheme-total-score-rule="
                            cultureSchemeTotalScoreRule
                        "
                        :is-graduate="isGraduate || isWorked"
                        :is-worked="isWorked"
                        :is-completed ="isCompleted"
                    />
                </el-card>
            </div>
            <div v-else>
                <div style="margin-bottom:24px">
                    <el-breadcrumb separator-class="el-icon-arrow-right">
                        <el-breadcrumb-item @click="previewPlan">
                            <span
                                class="color-main"
                                @click="previewPlan"
                            >{{ $t("culturePlan.myExecutionPlan") }}</span
                            >
                        </el-breadcrumb-item>
                        <el-breadcrumb-item>{{ title }}</el-breadcrumb-item>
                    </el-breadcrumb>
                </div>
                <el-card
                    shadow="never"
                    body-style="padding: 24px;"
                    class="card-margin"
                >
                    <h1 class="condition-title">{{ $t("common.text.baseInfo") }}</h1>
                    <div class="plan-photo">
                        <img src="/static/images/manage.png" >
                        <span>
                            {{ studentCultureRel ? language=='en'?studentCultureRel.nameSpelling:studentCultureRel.name : "" }}
                        </span>
                    </div>
                    <el-form label-position="right">
                        <el-row :gutter="24">
                            <el-col :span="6">
                                <el-form-item :label="$t('culturePlan.stuNumber') + '：'">{{
                                    studentCultureRel
                                        ? studentCultureRel.studentId
                                        : ""
                                }}</el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item
                                    :label="$t('label.grade') + '：'"
                                >{{
                                    studentCultureRel
                                        ? studentCultureRel.grade
                                        : ""
                                }}</el-form-item
                                >
                            </el-col>
                            <el-col :span="6">
                                <el-form-item
                                    :label="$t('label.college') + '：'"
                                >{{
                                    studentCultureRel
                                        ? studentCultureRel.departmentId
                                        : ""
                                }}</el-form-item
                                >
                            </el-col>
                            <el-col :span="6">
                                <el-form-item
                                    :label="$t('label.major') + '：'"
                                >{{
                                    studentCultureRel
                                        ? studentCultureRel.major
                                        : ""
                                }}</el-form-item
                                >
                            </el-col>
                        </el-row>
                        <el-row :gutter="24">
                            <el-col :span="6">
                                <el-form-item
                                    :label="$t('label.trainingLevel') + '：'"
                                >{{
                                    studentCultureRel
                                        ? studentCultureRel.trainingLevel
                                        : ""
                                }}</el-form-item
                                >
                            </el-col>
                            <el-col :span="6">
                                <el-form-item :label="$t('culturePlan.enterSchoolDate') + '：'">{{
                                    studentCultureRel
                                        ? studentCultureRel.enrolDate.substring(
                                            0,
                                            10
                                        )
                                        : ""
                                }}</el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item
                                    :label="$t('culturePlan.durationStudy') + '：'"
                                >
                                    {{ studentCultureRel ? studentCultureRel.lengthSchooling : "" }}
                                    {{ $t('culturePlan.year') }}
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item :label="$t('culturePlan.supervisor') + '：'">
                                    {{ language=='en'?(studentCultureRel?studentCultureRel.teacherNameEn:''):(studentCultureRel?studentCultureRel.teacherName:'') }}
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </el-card>
                <el-card
                    shadow="never"
                    body-style="padding: 24px;"
                    class="card-margin"
                >
                    <h1 class="condition-title">{{ $t("culturePlan.formulationPlan") }}</h1>
                    <graduate-plan
                        v-if="isGraduate"
                        :data="data"
                        :degree-type="degreeType"
                        :training-category-code="trainingCategoryCode"
                        :old4m3="old4m3"
                        :is-pass="isPass"
                        :student-culture-rel="studentCultureRel"
                        @showPlan="backMyPlan"
                    />
                    <graduateWorkedPlan
                        v-if="isWorked"
                        :data="data"
                        :old4m3="old4m3"
                        :is-pass="isPass"
                        :student-culture-rel="studentCultureRel"
                        @showPlan="backMyPlan"
                    />
                    <undergraduate-plan
                        v-if="!isGraduate && !isWorked"
                        :data="data"
                        :student-culture-rel="studentCultureRel"
                        :culture-plan-list-length="culturePlanListLength"
                        @showPlan="backMyPlan"
                    />
                </el-card>
            </div>
        </div>
    </div>
</template>
<script>
import { mapState } from 'vuex'
import {
    findStudentCultureRelById,
    getCulturePlanByStudentId,
    studentPlanCountByStuId,
    exportMyCulturePlanPdf,
    getPower,
    findAuthPushTimeAllPermission,
    updateStatus
} from 'common/src/api/culture/culturePlan'
import graduatePlan from './graduatePlan'
import graduateWorkedPlan from './graduateWorkedPlan'
import undergraduatePlan from './undergraduatePlan'
import scoreStatistics from './scoreStatistics'
import { findTeacherInfoBycode } from 'common/src/api/user'
import mixin from 'common/src/mixins/index'
export default {
    name: 'PersonalPlanDetail',
    components: {
        graduatePlan,
        undergraduatePlan,
        scoreStatistics,
        graduateWorkedPlan
    },
    mixins: [mixin],
    props: {
        studentId: {
            type: String,
            default: null
        },
        readOnly: {
            type: Boolean,
            default: false
        },
        approveDetail: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            uid: '',
            curType: null, // 当前登录用户type
            isRead: true, // 只读时，才显示基本信息
            studentCultureRel: null, // 当前学生培养计划信息
            culturePlanListLength: 0,
            success: false, // 完成加载
            customized: false, // 是否已制定培养计划
            isGraduate: false, // 是否研究生
            data: null, // 业务数据，包含培养方案、个人计划、模板规则等信息
            title: '',
            isChecking: false,
            courses: [],
            newLabel: [],
            labels: null,
            showBtn: false,
            showChooseCourseCrossFaultyDialog: false,
            supervisorName: '',
            showOperateButton: false,
            old4m3: '',
            role: '',
            timeOver: [],
            hasPlan: false,
            cultureSchemeTotalScoreRule: '',
            isWorked: false,
            trainingCategoryCode: '', // 当前培养方案所属培养类别编码，用于特定培养类别（“课程进修生”或“旁听生”“研修生”）所属的培养方案，提交保存时不受系统模板学分要求限制
            isCompleted: true, // 保存当前培养计划学分完成情况
            degreeType: ''
        }
    },
    computed: {
        language() {
            return this.$store.getters.language
        },
        ...mapState({
            mockUser: state => state.session.mockUser,
            user: state => state.session.user
        })
    },
    mounted: function() {
    // 属性studentId优先级高于url的Id
        let uid = null
        if (this.studentId) {
            uid = this.studentId
        } else if (this.$route.params && this.$route.params.id !== 'id') {
            uid = this.$route.params.id
            if (!uid) {
                uid = this.global.currentUser.uid
            }
        } else {
            uid = this.mockUser ? this.mockUser.uid : this.user.uid
        }
        const pathname = window.location.href
        if (pathname.indexOf('id') != -1 && !this.approveDetail) {
            this.hasPlan = true
        }
        if (pathname.indexOf('id') == -1 && this.approveDetail) {
            this.hasPlan = false
        }
        this.uid = uid
        this.setOperateButtonVisible()
        this.requestData()
        this.getScoreData()
        this.getRole()
    },
    methods: {
        recallAppro() {
            updateStatus(this.studentCultureRel.studentId).then(res => {
                if (res.code == 200) {
                    this.isChecking = false
                    this.$message({
                        type: 'success',
                        message: this.$t('culturePlan.recallApproSuccess')
                    })
                } else {
                    this.$message({
                        type: 'warning',
                        message: this.$t('culturePlan.recallApproFail')
                    })
                }
            })
        },
        setOperateButtonVisible() {
            const currentUser =
                (this.$store.state.session && this.$store.state.session.user) ||
                JSON.parse(localStorage.getItem('user'))
            this.showOperateButton =
                currentUser.currentRole == 1 || currentUser.type == 2
        },
        getRole() {
            getPower().then(res => {
                this.role = res.data.power
            })
        },
        getTimeOver(checkData) {
            findAuthPushTimeAllPermission(checkData).then(res => {
                this.timeOver = res.data
            })
        },
        requestData() {
            const _this = this
            _this.curType = _this.$store.getters.getUserType() // 获取当前登录用户type
            // console.log('_this.$store.getters.getUserType():',_this.$store.getters.getUserType());
            findStudentCultureRelById(_this.uid)
                .then(res => {
                    if (res.code == 200 && res.data) {
                        _this.degreeType = res.data.degreeType
                        _this.isPass = res.data.statusPlan == 3 ? 1 : 0
                        _this.studentCultureRel = res.data
                        _this.trainingCategoryCode =
                            res.data.trainingCategoryCode // 保存当前培养方案所属培养类别编码
                        _this.old4m3 = res.data.old4m3 // 保存老数据导入状态 1：老数据导入 0：新数据导入
                        _this.isChecking =
                            res.data.statusPlan == 1 // 状态1为审批中
                        _this.isGraduate = res.data.projId == 2
                        _this.isWorked = res.data.projId == 4
                        _this.showBtn = !!res.data.remarks // remarks是否显示按钮
                        _this.customized = !!res.data.passHistory // passHistory是否存在审批通过记录
                        // if (_this.studentCultureRel.teacherId) {
                        //     _this.__getSupervisor(
                        //         _this.studentCultureRel.teacherId
                        //     )
                        // }
                        // let time = new Date().getTime();
                        if (res.data.trainingCategory == '学位生') {
                            res.data.trainingCategory = 1
                        }
                        if (res.data.trainingCategory == '双学位生') {
                            res.data.trainingCategory = 2
                        }
                        if (res.data.trainingCategory == '学历学位生') {
                            res.data.trainingCategory = 3
                        }
                        if (res.data.trainingCategory == '旁听生') {
                            res.data.trainingCategory = 4
                        }
                        if (res.data.trainingCategory == '借读生') {
                            res.data.trainingCategory == 5
                        }
                        if (res.data.trainingCategory == '课程进修生') {
                            res.data.trainingCategory = 6
                        }
                        if (res.data.trainingCategory == '研修生') {
                            res.data.trainingCategory = 7
                        }
                        if (res.data.trainingCategory == '直博生') {
                            res.data.trainingCategory = 8
                        }
                        if (res.data.degreeType == '学术型') {
                            res.data.degreeType = 1
                        }
                        if (res.data.degreeType == '专业型') {
                            res.data.degreeType = 2
                        }
                        if (res.data.degreeType == '无学位') {
                            res.data.degreeType = 3
                        }
                        if (res.data.degreeType == '全部') {
                            res.data.degreeType = 4
                        }
                        if (res.data.formLearning == '全日制' || res.data.formLearning == 'Full-time') {
                            res.data.formLearning = 1
                        }
                        if (res.data.formLearning == '非全日制') {
                            res.data.formLearning = 2
                        }
                        if (res.data.formLearning == '全部') {
                            res.data.formLearning = 4
                        }
                        const checkData = {
                            grade: res.data.grade,
                            trainingLevel: res.data.trainingLevel == '硕士' ? '4' : '6',
                            term: res.data.enrolSeason,
                            degreeType: res.data.degreeType ? res.data.degreeType : '4',
                            formLearning: res.data.formLearning,
                            trainingCategory: res.data.trainingCategory,
                            projId: res.data.projId,
                            faculty: res.data.departmentCode || ''
                            // "currentTime": time
                        }
                        _this.getTimeOver(checkData)
                        if (_this.studentCultureRel.cultureId) {
                            return res.data.studentId
                        }
                    }
                })
                .catch(err => {
                    if (err) {
                        _this.hasPlan = true
                    }
                })
                .then(studentId => {
                    // 学生绑定培养方案,请求培养方案或培养计划信息
                    if (studentId) {
                        getCulturePlanByStudentId(studentId, _this.isPass).then(
                            res => {
                                if (res.code == 200) {
                                    _this.isCompleted = res.data && res.data.isCompleted
                                    _this.hasPlan = false
                                    _this.success = true
                                    _this.data = res.data
                                    _this.cultureSchemeTotalScoreRule =
                                        res.data.cultureSchemeTotalScoreRule
                                    _this.labels = _.cloneDeep(
                                        res.data.coursesLabelList
                                    )
                                    _this.culturePlanListLength =
                    res.data &&
                    res.data.culturePlanList.length > 0
                        ? res.data.culturePlanList.length
                        : 0
                                    let list = []
                                    if (_this.isGraduate) {
                                        if (_this.old4m3 * 1 == 1) {
                                            list =
                        res.data &&
                        res.data.culturePlanList
                            .length > 0
                            ? res.data.culturePlanList
                            : res.data
                                .cultureCourseLabelRelationList
                                        } else {
                                            list =
                        res.data &&
                        res.data.culturePlanList
                                        }
                                    } else {
                                        list =
                      res.data &&
                      res.data.culturePlanList.length > 0
                          ? res.data.culturePlanList
                          : res.data
                              .cultureCourseLabelRelationList
                                    }
                                    _this.courses = _.cloneDeep(list)
                                }
                            }
                        )
                    }
                })
        },
        getScoreData() {
            studentPlanCountByStuId(this.uid).then(res => {
                if (res.code == 200) {
                    this.newLabel = res.data
                }
            })
        },
        customPlan() {
            const time = new Date().getTime()
            let isTrue = true
            this.timeOver.forEach(row => {
                if (
                    (row.startTime > time || row.endTime < time) &&
                    this.role !== '3' &&
                    this.role !== '4'
                ) {
                    this.$message({
                        type: 'warning',
                        message: this.$t('culturePlan.accessTimePrevented')
                    })
                    isTrue = false
                }
            })
            if (!isTrue) {
                return
            }
            this.requestData()
            this.isRead = false
            this.title = this.customized ? this.$t('culturePlan.changePlan') : this.$t('culturePlan.createPlan')
        },
        previewPlan() {
            this.requestData()
            this.isRead = true
        },
        backMyPlan(val) {
            if (val) {
                this.requestData()
                this.isRead = true
            }
            this.getScoreData()
        },
        explorerPlan() {
            const type = ''
            exportMyCulturePlanPdf(this.uid, type)
        },
        explorerWordsPlan() {
            const type = 1
            exportMyCulturePlanPdf(this.uid, type)
        },
        culturePreview() {
            const schemeDetails = {
                projId: this.studentCultureRel && this.studentCultureRel.projId,
                id: this.studentCultureRel.cultureId,
                isRead: true,
                dataType: '2',
                nameCN: '',
                nameEN: ''
            }
            sessionStorage.setItem(
                'schemeDetails',
                JSON.stringify(schemeDetails)
            )
            this.$router.push({
                name: 'cultureSchemePreview',
                params: {
                    projId:
                        this.studentCultureRel && this.studentCultureRel.projId,
                    id: this.studentCultureRel.cultureId,
                    isRead: true,
                    dataType: '2'
                }
            })
        }
        // 获取导师信息
        // __getSupervisor(teacherId) {
        //     findTeacherInfoBycode(teacherId).then(res => {
        //         if (res && res.code === 200 && res.data) {
        //             this.supervisorName = res.data.name || ''
        //         }
        //     })
        // }
    }
}
</script>
<style lang="scss" scoped>
.no-culture-plan {
    background: url("/static/images/no-cultureplan.png");
    width: 444px;
    height: 249px;
    margin: 20px auto 40px;
}
.plan-table {
    width: 100%;
    border: 1px solid #dadcde;
    border-right: 0;
    border-bottom: 0;
    border-spacing: 0;
    td {
        padding: 4px 10px;
        line-height: 24px;
        border-right: 1px solid #dadcde;
        border-bottom: 1px solid #dadcde;
    }
    td.right {
        text-align: right;
    }
    white-space: normal;
}
.plan-photo {
    margin: 0 1px 20px;
    padding-bottom: 15px;
    border-bottom: 1px solid #ccc;
    img {
        width: 36px;
        height: 36px;
        border-radius: 50%;
        vertical-align: -12px;
    }
    span {
        margin-left: 15px;
        line-height: 36px;
    }
}
</style>
